<template>
  <div class="app-container">
    讲师列表
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="courseQuery.title" placeholder="课程标题"/>
      </el-form-item>

      <el-form-item>
        <el-select v-model="courseQuery.teacherId" clearable placeholder="授课讲师">
          <el-option v-for="item in teacherList" :key="item.id" :value="item.id" :label="item.name"/>
        </el-select>
      </el-form-item>

      <el-form-item label="课程分类">
        <el-select v-model="courseQuery.subjectParentId" clearable placeholder="课程分类">
          <el-option v-for="item in subjectOneList" :key="item.id" :value="item.id" :label="item.title"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="courseQuery.subjectId" clearable>
          <el-option v-for="item in subjectTwoList" :key="item.id" :value="item.id" :label="item.title"/>
        </el-select>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
      row-class-name="myClassList">
      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="课程信息" width="470" align="center">
        <template slot-scope="scope">
          <div class="info">
            <div class="pic">
              <img :src="scope.row.cover" :alt="scope.row.title" width="150px">
            </div>
            <div class="title">
              <a href="">{{ scope.row.title }}</a>
              <p>{{ scope.row.lessonNum }}课时</p>
            </div>
          </div>

        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.gmtCreate.substr(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.gmtModified.substr(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column label="价格" width="100" align="center" >
        <template slot-scope="scope">
          {{ Number(scope.row.price) === 0 ? '免费' :
          '¥' + scope.row.price.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column prop="buyCount" label="付费学员" width="100" align="center" >
        <template slot-scope="scope">
          {{ scope.row.buyCount }}人
        </template>
      </el-table-column>

      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <router-link :to="'/edu/course/info/'+scope.row.id">
            <el-button type="text" size="mini" icon="el-icon-edit">编辑课程信息</el-button>
          </router-link>
          <router-link :to="'/edu/course/chapter/'+scope.row.id">
            <el-button type="text" size="mini" icon="el-icon-edit">编辑课程大纲</el-button>
          </router-link>
          <el-button type="text" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"/>

  </div>
</template>
<script>
  import course from '@/api/edu/course';
  import teacher from '@/api/edu/teacher';
  import subject from '@/api/edu/subject';

  export default {
    data() { //定义变量和初始值
      return {
        isRed: false,
        listLoading: false,
        list: null,//查询之后接口返回集合
        page: 1,//当前页
        limit: 10,//每页记录数
        total: 0,//总记录数
        courseQuery: {}, //条件封装对象
        teacherList: [],
        subjectOneList: [],
        subjectTwoList: []
      }
    },
    created() { //页面渲染之前执行，一般调用methods定义的方法
      //调用
      this.getList();
      this.getAllSubject();
      this.getAllTeacher();
    },
    methods: {  //创建具体的方法，调用teacher.js定义的方法
      //讲师列表的方法
      getList(page = 1) {
        this.listLoading = true;
        this.page = page
        course.pageCourseCondition(this.page, this.limit, this.courseQuery).then(res => {//请求成功
          this.list = res.data.rows;
          this.total = res.data.total;
          this.listLoading = false;
        })
      },
      resetData() {//清空的方法
        //表单输入项数据清空
        this.courseQuery = {}
        //查询所有讲师数据
        this.getList()
      },
      /**
       * 删除讲师的方法
       */
      removeDataById(id) {
        console.log(id)
        this.$confirm('此操作将永久删除课程记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {  //点击确定，执行then方法
          //调用删除的方法
          course.deleteCourse(id).then(res => {//删除成功
            //提示信息
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            //回到列表页面
            this.getList()
          })
        }) //点击取消，执行catch方法
      },
      /**
       * 获取所有讲师信息
       */
      getAllTeacher() {
        teacher.findAll().then(res => {
          this.teacherList = res.data.items;
        })
      },
      getAllSubject() {
        subject.getSubjectListPage().then(res => {
          this.subjectOneList = res.data.list;
        })
      }
    },
    watch: {
      'courseQuery.subjectParentId': function(val) {
        if (val) {
          this.courseQuery.subjectId = '';
          let list = this.subjectOneList.filter(item => item.id === val);
          this.subjectTwoList = list[0].children;
        }
      }
    }
  }
</script>

<style>
  .text-red {
    color: red;
  }
</style>
